package com.ly.demo.entity;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author liuyang
 * @Date 2024/3/10 21:29
 **/
@Data
public class OrderEntity {
    /**
     * 预计开始时间
     */
    private LocalDateTime effectiveTime;

    /**
     * 预计结束时间
     */
    private LocalDateTime expireTime;

    public static void main(String[] args) {
        OrderEntity firstOrder = new OrderEntity();
        firstOrder.setEffectiveTime(DateUtil.parse("2024-04-01 11:11:11").toLocalDateTime());
        firstOrder.setExpireTime(DateUtil.parse("2024-04-09 11:11:11").toLocalDateTime());


        OrderEntity firstOrder2 = new OrderEntity();
        firstOrder2.setEffectiveTime(DateUtil.parse("2024-05-09 11:11:11").toLocalDateTime());
        firstOrder2.setExpireTime(DateUtil.parse("2024-06-09 11:11:11").toLocalDateTime());


        OrderEntity firstOrder3 = new OrderEntity();
        firstOrder3.setEffectiveTime(DateUtil.parse("2024-07-09 22:22:22").toLocalDateTime());
        firstOrder3.setExpireTime(DateUtil.parse("2025-04-09 22:22:22").toLocalDateTime());

        OrderEntity firstOrder4 = new OrderEntity();
        firstOrder4.setEffectiveTime(DateUtil.parse("2025-06-09 11:11:11").toLocalDateTime());
        firstOrder4.setExpireTime(DateUtil.parse("2025-08-09 11:11:11").toLocalDateTime());
        ArrayList<OrderEntity> list = ListUtil.toList(firstOrder, firstOrder2, firstOrder3, firstOrder4);

        System.out.println(list);
        processOrders(list);
        System.out.println(list);


    }

    public static void processOrders(List<OrderEntity> orderList) {
        // 处理第一个订单
        if (CollUtil.isNotEmpty(orderList)) {
            OrderEntity firstOrder = orderList.get(0);
            LocalDateTime currentDateTime = LocalDateTime.now();
            long day = DateUtil.betweenDay(DateUtil.date(firstOrder.getEffectiveTime()), DateUtil.date(firstOrder.getExpireTime())
                    , true);
            if (firstOrder.getEffectiveTime().isAfter(currentDateTime)) {
                // 如果第一个订单的开始时间在当前时间之前后，将预计开始时间改为当前时间
                firstOrder.setEffectiveTime(currentDateTime);
                firstOrder.setExpireTime(DateUtil.offset(DateUtil.date(currentDateTime), DateField.DAY_OF_MONTH, Convert.toInt(day)).toLocalDateTime());
            }

            // 处理后续订单
            for (int i = 1; i < orderList.size(); i++) {
                day = DateUtil.betweenDay(DateUtil.date(orderList.get(i).getEffectiveTime()), DateUtil.date(orderList.get(i).getExpireTime())
                        , true);
                OrderEntity previousOrder = orderList.get(i - 1);
                OrderEntity currentOrder = orderList.get(i);

                // 将下一个订单的预计开始时间设置为上一个订单的预计结束时间
                currentOrder.setEffectiveTime(previousOrder.getExpireTime());
                currentOrder.setExpireTime(DateUtil.offset(DateUtil.date(previousOrder.getExpireTime()), DateField.DAY_OF_MONTH, Convert.toInt(day)).toLocalDateTime());

            }
        }
    }
}
